﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace NutriLeaf.Web.Core.Location
{
    public class Country:ICountry
    {
        public List<Model.Country> GetAllCountries(ref Model.NutriLeafDBEntities context)
        {
            return context.Countries.ToList();
        }

        public List<Model.Country> GetCountriesThatStartsWith(string startsWith, ref Model.NutriLeafDBEntities context)
        {
            // not known
            throw new NotImplementedException();
        }

        public Model.Country GetCountry(int id, ref Model.NutriLeafDBEntities context)
        {
            if (context.Countries.Where(p => p.CountryID == id).Count() == 0)
            {
                return context.Countries.Where(p => p.CountryID == id).First();
            }
            else
                return null;
        }

        public void InsertCountry(string countryName, ref Model.NutriLeafDBEntities context)
        {
            Model.Country county = new Model.Country();
            county.Name = countryName;
            context.Countries.AddObject(county);
            context.SaveChanges();
        }

        public void InsertCountry(Model.Country country, ref Model.NutriLeafDBEntities context)
        {
            context.Countries.AddObject(country);
            context.SaveChanges();
        }

        public void UpdateCountry(int countryId, string countryName, ref Model.NutriLeafDBEntities context)
        {
            Model.Country country = new Model.Country();
            country.Name = countryName;
            country.CountryID = countryId;
            context.Countries.AddObject(country);
            context.SaveChanges();
        }

        public void RemoveCountry(int countryId, ref Model.NutriLeafDBEntities context)
        {
            if (context.Countries.Where(p => p.CountryID == countryId).Count() != 0) {
                context.DeleteObject(context.Countries.Where(p => p.CountryID == countryId).First());
                context.SaveChanges();
            }
        }

        public void RemoveCountry(string countryName, ref Model.NutriLeafDBEntities context)
        {
            if (context.Countries.Where(p => p.Name.CompareTo(countryName)==0).Count() != 0)
            {
                context.DeleteObject(context.Countries.Where(p => p.Name.CompareTo(countryName)==0).First());
                context.SaveChanges();
            }
        }

        public void RemoveCountry(Model.Country country, ref Model.NutriLeafDBEntities context)
        {
            if (context.Countries.Where(p => p.CountryID == country.CountryID).Count() != 0)
            {
                context.DeleteObject(context.Countries.Where(p => p.CountryID ==country.CountryID).First());
                context.SaveChanges();
            }
        }
    }
}